#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

#define SQLITE3_DB_NAME "05_test.db"

int Callback( void* Data, int argc, char **argv, char **ColomnName )
{
    int i;

    static char NameFlag = 0;
    if ( 0 == NameFlag )
    {
        for( i = 0; i < argc; ++i )
        {
            printf( "%s\t", ColomnName[i] );
        }
        printf( "\n" );
        NameFlag = 1;
    }

    for( i= 0; i < argc; ++i )
    {
        printf( "%s\t", argv[i] ? argv[i] : "NULL" );
    }
    printf( "\n" );

    return 0;
}
int main(int argc, char const *argv[])
{
    sqlite3 *dbClass = NULL;
    char *strErrMsg = NULL;
    int Ret=0;
    char *SqlCreateStatements;
    char *SqlInsertStatements;
    char *SqlSelectStatements;
    char *SqlUpdateStatements;

    Ret = sqlite3_open( SQLITE3_DB_NAME, &dbClass );
    if ( Ret != SQLITE_OK )
    {
        fprintf( stderr, "fail sqlite3_open: %s\n", sqlite3_errmsg( dbClass ) );
        exit(0);
    }

    SqlCreateStatements = "CREATE TABLE if not exists TEAM_01("
                    "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                    "NAME TEXT,"
                    "SCORE REAL);";

    Ret = sqlite3_exec( dbClass, SqlCreateStatements, Callback, 0, &strErrMsg );
    if ( Ret != SQLITE_OK )
    {
        fprintf( stderr, "fail sqlite3_exec create: %s\n", strErrMsg );
        sqlite3_free( strErrMsg );
    }

    SqlInsertStatements = "INSERT INTO TEAM_01 VALUES(NULL,'chen',86);"
                          "INSERT INTO TEAM_01 VALUES(NULL,'zhao',72);"
                          "INSERT INTO TEAM_01 VALUES(8,'gao',81);";
    Ret = sqlite3_exec( dbClass, SqlInsertStatements, Callback, 0, &strErrMsg );
    if ( Ret != SQLITE_OK )
    {
        fprintf( stderr, "fail sqlite3_exec insert: %s\n", strErrMsg );
        sqlite3_free( strErrMsg );
    }

    SqlUpdateStatements = "UPDATE TEAM_01 set SCORE=66 where ID=8";
    Ret = sqlite3_exec( dbClass, SqlUpdateStatements, Callback, 0, &strErrMsg );
    if ( Ret != SQLITE_OK )
    {
        fprintf( stderr, "fail sqlite3_exec insert: %s\n", strErrMsg );
        sqlite3_free( strErrMsg );
    }

    SqlSelectStatements = "SELECT * FROM TEAM_01";
    Ret = sqlite3_exec( dbClass, SqlSelectStatements, Callback, 0, &strErrMsg );
    if ( Ret != SQLITE_OK )
    {
        fprintf( stderr, "fail sqlite3_exec select: %s\n", strErrMsg );
        sqlite3_free( strErrMsg );
    }

    sqlite3_close( dbClass );

    printf( "success create sqlite3 db: %s\n", SQLITE3_DB_NAME );

    return 0;
}

/*
程序功能
    创建 SQLITES3数据库文件 05_test.db
    创建 数据表 TEAM_01
    TEAM_01 中包括 ID, NAME, SCORE
    在 TEAM_01 中插入 3个数据记录
    更新一个数据记录
    调用 SELECT 命令, 查询数据记录

涉及函数
    sqlite3_open()
    sqlite3_create()
    sqlite3_exec()
        CREATE TABLE
        INSERT INTO VALUES
        UPDATE
        SELECT *
    sqlite3_free()
    sqlite3_close()

知识要点
    sqlite3 创建 数据库文件 和 数据表, 插入数据, 查询数据 的 原子操作

测试流程
    sqlite3 05_test.db
    select * from TEAM_01;
    .quit
    sqlite3 05_test.db .dump > 05_test.sql;

*/
